//
//  GNStatusViewModel.swift
//  SinaWeChat
//
//  Created by mac on 15/12/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

// HMStatus 里面需要处理逻辑的代码都放在这个地方
// 比如说,格式化来源的字符串,以及时间字符串,以及其他

class GNStatusViewModel: NSObject {
    
    var status:GNStatus?{
        
        didSet{
            //外界设置此值时才会调用此方法
        }
    }
    
    //判断会员等级到底用和图标
    
    var vipImage:UIImage?{
        
        // 判断用户的等级,如果大于0小于7
        // 就返回一个对应会员的图标
        
        guard let mbrank = status?.user?.mbrank else{
            
            return nil
            
        }
        
        if mbrank > 0 && mbrank < 7 {
            
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return nil
        
    }
    
    //认证的图标
    //认证类型 -1：没有认证; 1:认证用户，2,3,5: 企业认证，220: 达人
    
    var verifiedImage:UIImage? {
        
        guard let verified = status?.user?.verified else{
            return nil
        }
        
        switch verified {
        case 1:
            
            return UIImage(named: "avatar_vip")
            
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default :return nil
            
        }
        
        
    }
    
    //显示转发评论赞 的数量
    
    var retweetCountStr: String = "转发"
    var commentCountStr: String = "评论"
    var attitudeCountStr: String = "赞"
    
    //转发微博的内容
    var retweetText: String?
<<<<<<< HEAD
    
    //来源字符串
    var sourceText:String?
    
    //创建时间
    var creatAtText:String{
        
        //已提取到 NSDate + Extension
        guard let creatDate = NSDate.sinaDate(status!.created_at!)
        else{
            
            return ""
        }
        
        return creatDate.sinaDateString
    }
=======
>>>>>>> 009be840054e3af11221fb27da600fe2ee052d08
    
    init(status:GNStatus) {
        
        super.init()
        
        self.status  = status
        
        retweetCountStr = getCountString(self.status?.reposts_count ?? 0, defaultTitle: "转发")
        
        commentCountStr = getCountString(self.status?.comments_count ?? 0,defaultTitle: "评论")
        attitudeCountStr = getCountString(self.status?.attitudes_count ?? 0, defaultTitle: "赞")
        
        // 计算转发微博内容的逻辑
        if let retweetStatus = status.retweeted_status {
            
            if let name = retweetStatus.user?.name {
                
                retweetText = "@\(name):\(retweetStatus.text!)"
            }
        }
        
<<<<<<< HEAD
        //判断 source  是否存在
        if let source = status.source where source.containsString("\">"){
            
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            
            sourceText = "来自\(source.substringWithRange(startRange.endIndex..<endRange.startIndex))"
        }
        
        
=======
>>>>>>> 009be840054e3af11221fb27da600fe2ee052d08
    }
    
    
    
    private func getCountString(count:Int,defaultTitle:String) -> String{
        
        // count 11010 --> "1.1万"
        // count 10900 --> "1万"
        if count > 0{
            
            //小于1万显示元数据
            
            if count < 10000{
                return "\(count)"
            }else{
                
                let result = CGFloat(count/1000)/10
                
                let resultStr = "\(result)万"
                
                if resultStr.containsString(".0")
                {
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
            }
        }
        
        return defaultTitle
    }
    
    override var description:String {
        
        let keys = ["status"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
}